package com.shardingsphere.shardingsphere.myShardingAlgorithm;

import groovy.util.logging.Slf4j;
import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import sun.applet.Main;

import java.util.Collection;
import java.util.HashMap;
import java.util.UUID;

@Slf4j
public class TableAlgorithm implements PreciseShardingAlgorithm<String> {

	@Override
	public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {


		for (String tableName : collection) {

			if (tableName.endsWith(hash(preciseShardingValue.getValue()) % 2 + "")) {
				return tableName;
			}
		}
		throw new IllegalArgumentException();


	}

	static  int hash(Object key) {
		int h;
		return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
	}


	public static void main(String[] args) {
		DBAlgorithm.main(new String[]{"1"});
	}

}
